<?php


namespace App\Models\CacheModel\active;


use app\common\lib\Util;
use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Active1UserHelp extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.active1_user_help';

    protected $cacheModel = 'active.user_help';

    /**
     * 更新助力信息
     * @param string $id
     * @param array $data
     * @return bool
     */
    public function UpdateData(string $id, array $data)
    {
        try
        {
            $this->where('id', $id)->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->setCache($data);
            return true;
        }
        catch (\Exception $e)
        {
            Log::write('Active1UserHelp model => UpdateData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

            return false;
        }
    }

    /**
     * 获取用户助力详情
     * @param int $activeFlag
     * @param int $uid
     * @param int $type
     * @return array
     * @throws \Exception
     */
    public function getUserHelpDetails(int $activeFlag, int $uid, int $type)
    {
        $id = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('user_help_id', $activeFlag . ':' . $uid . ':' . $type)
            ->setCacheType('string')
            ->getDataAndSet(function () use ($activeFlag, $uid, $type)
            {
                try
                {
                    return $this->where(['active_flag' => $activeFlag, 'uid' => $uid, 'flag' => $type])->value('id');
                }
                catch (\Exception $e)
                {
                    Log::write('Active1UserHelp model => getUserHelpDetails()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
                    return 0;
                }
            }, [], 'user_help_id');

        if ($id)
        {
            return $this->getInfoById($id);
        } else
        {
            $init = [
                'id' => Util::getStringRandomId(),
                'active_flag' => $activeFlag,
                'help_num' => 0,
                'cid_type' => 0,
                'cid' => 0,
                'uid' => $uid,
                'flag' => $type,
                'create_time' => time(),
                'end_time' => 0
            ];

            $this->save($init);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $init['id'])->setCache($init, 'details', false);

            return $init;
        }
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        $field = 'active_flag,uid,help_num,cid_type,cid,flag,create_time,end_time';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            $resData[$orderId] = arrayKeyData2int($orderItemData, ['active_flag', 'uid', 'help_num', 'cid_type', 'cid', 'flag', 'create_time', 'end_time']);
        }

        return $resData;
    }
}
